﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Scheduler;
using System.IO.IsolatedStorage;

namespace PhoneApp1
{
    public partial class AddNotification : PhoneApplicationPage
    {

        // Use Isolated Storage to store the alarm solve puzzle type
        private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

        public AddNotification()
        {
            InitializeComponent();
  
            // ISSUE #16
            // https://code.google.com/p/student-alarm-clock/issues/detail?id=16
            // Set the default time for the alarm to go off 
            // as the current time plus 1 minute.
            DateTime defaultDate = new DateTime();
            defaultDate = DateTime.Now;
            beginTimePicker.Value = defaultDate.AddMinutes(1);    
        
        }

        private void ApplicationBarSaveButton_Click(object sender, EventArgs e)
        {
            // Generate a unique name for the new notification. You can choose a
            // name that is meaningful for your app, or just use a GUID.
            String name = System.Guid.NewGuid().ToString();

            // Get the begin time for the notification by combining the DatePicker
            // value and the TimePicker value.
            DateTime date = (DateTime)beginDatePicker.Value;
            DateTime time = (DateTime)beginTimePicker.Value;

            // Store the overall begin time
            DateTime beginTime = date + time.TimeOfDay;



            // Make sure that the begin time has not already passed.
            if (beginTime < DateTime.Now)
            {
                MessageBox.Show("ERROR: The begin date must be in the future!");
                return;
            }


            // Right, store the type of alarm
            // There's the sequence type, math type and none type

            // Math puzzle
            if (mathRadioButton.IsChecked == true)
            {
                // If the key already exists update it, otherwise add it
                if (appSettings.Contains("puzzle_type"))
                {
                    appSettings["puzzle_type"] = "math";
                }
                else
                {
                    appSettings.Add("puzzle_type", "math");
                }
            }
                // Sequence Puzzle.
            else if (sequenceRadioButton.IsChecked == true)
            {
                if (appSettings.Contains("puzzle_type")) // Key already exists - update it
                {
                    appSettings["puzzle_type"] = "sequence";
                }
                else
                {
                    appSettings.Add("puzzle_type", "sequence");
                }
            }/*
            // If the user presses the NONE radio button
            // then no alarm will display
            else if (noneRadioButton.IsChecked == true)
            {
                if (appSettings.Contains("puzzle_type"))
                {
                    appSettings["puzzle_type"] = "none";
                }
                else
                {
                    appSettings.Add("puzzle_type", "none");
                }
                // Do this later.
            }*/
            

            // OK since Windows Phone doesn't really have an effective method
            // for tracking things in the background WITHOUT draining the battery,
            // we create 50 instances of this alarm (gulp).

            for (int i = 0; i < 50; i++)
            {

                Alarm alarm = new Alarm("student_alarm_clock_" + i);
                //      alarm.Title = "Student Alarm Clock";
                alarm.Content = "Student Alarm Clock\nTo deactive the alarm, open the Student Alarm Clock app and solve the puzzle.";
                alarm.BeginTime = beginTime.AddSeconds(i * 10);
                alarm.ExpirationTime = alarm.BeginTime.AddSeconds(5.0);
                alarm.RecurrenceType = RecurrenceInterval.None;
                alarm.Sound = new Uri("Alarm_Sound.mp3", UriKind.Relative);
                

                ScheduledActionService.Add(alarm);
            }           

            // Navigate back to the main reminder list page.
            NavigationService.GoBack();
        }
    }
}